﻿using System;
using System.Collections.Generic;
using System.Text;
using Bosphorus.Migrator.Core.MigrationCommand;
using Bosphorus.Migrator.Dialect.Core;

namespace Bosphorus.Migrator.Dialect.ViewEngine
{
    public class ViewEngineDialectAdapter<TModel> :  AbstractDialect<AbstractCommand<TModel>>
    {
        private readonly IViewEngine viewEngine;
        private readonly TemplatePathBuilder templatePathFounder;

        public ViewEngineDialectAdapter(IViewEngine viewEngine, TemplatePathBuilder templatePathFounder)
        {
            this.viewEngine = viewEngine;
            this.templatePathFounder = templatePathFounder;
        }

        public override string Create(AbstractCommand<TModel> model)
        {
            return Evalueate(model);
        }

        public override string Alter(AbstractCommand<TModel> model)
        {
            return Evalueate(model);
        }

        public override string Drop(AbstractCommand<TModel> model)
        {
            return Evalueate(model);
        }

        private string Evalueate(ICommand model)
        {
            string templatePath = templatePathFounder.Build<TModel>(model.Name);
            ViewEngineContext viewEngineContext = new ViewEngineContext(templatePath, model);
            string evaluatedResult = viewEngine.Evaluate(viewEngineContext);
            return evaluatedResult;
        }
    }
}
 